package admin

import (
	"baokaobang/datasourse"
	"baokaobang/models"
	"baokaobang/utils"
	"encoding/json"
	"fmt"
	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/mvc"
	"strings"
)

type AddAuthorityCategory struct {
	Id   string `json:"id"`
	Name string `json:"name"`
}

type AuthorityCategoryListParam struct {
	PerPage int `json:"perPage"`
	CurPage int `json:"curPage"`
}

type AuthorityCategory struct {
	Ctx iris.Context
}

func (c *AuthorityCategory) OptionsCreate() mvc.Response {
	return mvc.Response{
		Code: 204,
	}
}

func (c *AuthorityCategory) PostCreate() interface{} {
	addAuthorityCategory := AddAuthorityCategory{}
	content, err := c.Ctx.GetBody()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("接收请求参数出错:%s", err.Error()), "")
	}
	err = json.Unmarshal(content, &addAuthorityCategory)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("解析请求参数出错:%s", err.Error()), "")
	}

	if len(addAuthorityCategory.Name) <= 0 {
		return utils.CommonRes(1000, "请输入权限分类名称", "")
	}

	db, err := datasourse.NewMasterOrm()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("无法连接数据库:%s", err.Error()), "")
	}

	authorityCategory := new(models.AuthorityCategory)
	authorityCategory.Name = addAuthorityCategory.Name
	_, err = db.InsertOne(authorityCategory)

	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("操作失败:%s", err.Error()), "")
	}

	return utils.CommonRes(20000, "操作成功", "")
}

func (c *AuthorityCategory) OptionsEdit() mvc.Response {
	return mvc.Response{
		Code: 204,
	}
}

func (c *AuthorityCategory) PostEdit() interface{} {
	addAuthorityCategory := AddAuthorityCategory{}
	content, err := c.Ctx.GetBody()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("接收请求参数出错:%s", err.Error()), "")
	}
	err = json.Unmarshal(content, &addAuthorityCategory)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("解析请求参数出错:%s", err.Error()), "")
	}

	if len(addAuthorityCategory.Id) <= 0 {
		return utils.CommonRes(1000, "请提供权限分类id", "")
	}
	if len(addAuthorityCategory.Name) <= 0 {
		return utils.CommonRes(1000, "请输入权限分类名称", "")
	}

	db, err := datasourse.NewMasterOrm()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("无法连接数据库:%s", err.Error()), "")
	}

	updateData := map[string]interface{}{
		"name":    addAuthorityCategory.Name,
	}

	_, err = db.Table(new(models.AuthorityCategory)).ID(addAuthorityCategory.Id).Update(updateData)
	if err != nil {
		return utils.CommonRes(1000, "操作失败", "")
	}

	return utils.CommonRes(20000, "操作成功", "")
}

func (c *AuthorityCategory) OptionsAll() mvc.Response {
	return mvc.Response{
		Code: 204,
	}
}

func (c *AuthorityCategory) PostAll() interface{} {
	db, err := datasourse.NewMasterOrm()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("无法连接数据库:%s", err.Error()), "")
	}

	authorityCategoryList := make([]models.AuthorityCategory, 0)
	err = db.Find(&authorityCategoryList)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("无法获取权限分类列表:%s", err.Error()), "")
	}

	return utils.CommonRes(20000, "成功获取权限分类列表", authorityCategoryList)
}

func (c *AuthorityCategory) OptionsList() mvc.Response {
	return mvc.Response{
		Code: 204,
	}
}

func (c *AuthorityCategory) PostList() interface{} {
	param := AuthorityCategoryListParam{}
	content, err := c.Ctx.GetBody()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("接收请求参数出错:%s", err.Error()), "")
	}
	err = json.Unmarshal(content, &param)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("解析请求参数出错:%s", err.Error()), "")
	}

	db, err := datasourse.NewMasterOrm()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("无法连接数据库:%s", err.Error()), "")
	}

	start := (param.CurPage - 1) * param.PerPage
	authorityCategoryList := make([]models.AuthorityCategory, 0)
	sess := db.Limit(param.PerPage, start)
	err = sess.Find(&authorityCategoryList)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("无法获取权限分类列表:%s", err.Error()), "")
	}

	total, err := db.Where("id > ?", 0).Count(new(models.AuthorityCategory))
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("无法获取权限分类列表:%s", err.Error()), "")
	}

	for i, item := range authorityCategoryList {
		authorityCategoryList[i].AuthorityCategoryCreated = item.Created.Format("2006-01-02 15:04")
	}

	data := map[string]interface{}{
		"total": total,
		"list":  authorityCategoryList,
	}

	return utils.CommonRes(20000, "成功获取权限分类列表", data)
}

func (c *AuthorityCategory) OptionsDetail() mvc.Response {
	return mvc.Response{
		Code: 204,
	}
}

func (c *AuthorityCategory) PostDetail() interface{} {
	editParam := utils.EditParam{}
	content, err := c.Ctx.GetBody()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("接收请求参数出错:%s", err.Error()), "")
	}
	err = json.Unmarshal(content, &editParam)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("解析请求参数出错:%s", err.Error()), "")
	}
	if len(editParam.Id) <= 0 {
		return utils.CommonRes(1000, "参数不完整", "")
	}

	db, err := datasourse.NewMasterOrm()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("无法连接数据库:%s", err.Error()), "")
	}

	authorityCategory := new(models.AuthorityCategory)
	ok, err := db.ID(editParam.Id).Get(authorityCategory)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("信息获取失败:%s", err.Error()), "")
	}
	if !ok {
		return utils.CommonRes(1000, "信息获取失败", "")
	}

	return utils.CommonRes(20000, "信息获取成功", authorityCategory)
}

func (c *AuthorityCategory) OptionsDel() mvc.Response {
	return mvc.Response{
		Code: 204,
	}
}

func (c *AuthorityCategory) PostDel() interface{} {
	delParam := utils.DelParam{}
	content, err := c.Ctx.GetBody()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("接收请求参数出错:%s", err.Error()), "")
	}
	err = json.Unmarshal(content, &delParam)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("解析请求参数出错:%s", err.Error()), "")
	}

	if len(delParam.Ids) <= 0 {
		return utils.CommonRes(1000, "请提供要删除项的id", "")
	}

	db, err := datasourse.NewMasterOrm()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("无法连接数据库:%s", err.Error()), "")
	}

	ids := strings.Split(delParam.Ids, ",")
	for _, id := range ids {
		authorityCategory := new(models.AuthorityCategory)
		db.ID(id).Get(authorityCategory)
		db.ID(id).Delete(authorityCategory)
	}

	return utils.CommonRes(20000, "操作成功", "")
}
